#include <iostream>
#include <string>
#include <vector>
#include <stack>
using namespace std;

 struct TreeNode {
     int val;
     TreeNode *left;
      TreeNode *right;
      TreeNode(int x) : val(x), left(NULL), right(NULL) {}
  };

    vector<int> postorderTraversal(TreeNode *root) {
        vector<int> result;
        if(root == NULL){
            return result;
        }
        TreeNode *cur;
        stack<TreeNode*> list;
        list.push(root);
        while(!list.empty()){
            cur = list.top();
            if(cur->left != NULL){
                list.push(cur->left);
            }else{
                if(cur->right != NULL){
                    list.push(cur->right);
                }else{
                    list.pop();
                    result.push_back(cur->val);
                    if(!list.empty()){
                        if(list.top()->left == cur){
                            list.top()->left = NULL;
                        }else{
                            list.top()->right = NULL;
                        }
                    }
                    delete cur;
                }
            }
            
        }
        return result;
    }

int main(){
	TreeNode* root = new TreeNode(1);
	vector<int> result;
	result = postorderTraversal(root);
	cout<<result[0]<<endl;
	return 0;

}
